home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / rpg / crossfir.92 / crossfir / crossfire-0.92.5 / server / daemon.c < prev    next >
C/C++ Source or Header  |  1996-07-24  |  3KB  |  121 lines

  1. /*
  2.  * static char *rcsid_deamon_c =
  3.  *   "$Id: daemon.c,v 1.9 1996/03/06 06:37:56 master Exp $";
  4.  */
  5.  
  6. /*
  7.  * BecomeDaemon.c
  8.  * shamelessly swiped from xdm source code.
  9.  * Appropriate copyrights kept, and hereby follow
  10.  * ERic mehlhaff, 3/11/92
  11.  *
  12.  * xdm - display manager daemon
  13.  *
  14.  * $XConsortium: daemon.c,v 1.5 89/01/20 10:43:49 jim Exp $
  15.  *
  16.  * Copyright 1988 Massachusetts Institute of Technology
  17.  *
  18.  * Permission to use, copy, modify, and distribute this software and its
  19.  * documentation for any purpose and without fee is hereby granted, provided
  20.  * that the above copyright notice appear in all copies and that both that
  21.  * copyright notice and this permission notice appear in supporting
  22.  * documentation, and that the name of M.I.T. not be used in advertising or
  23.  * publicity pertaining to distribution of the software without specific,
  24.  * written prior permission.  M.I.T. makes no representations about the
  25.  * suitability of this software for any purpose.  It is provided "as is"
  26.  * without express or implied warranty.
  27.  *
  28.  * Author:  Keith Packard, MIT X Consortium
  29.  */
  30.  
  31.  
  32. #include <global.h>
  33. #include <sys/ioctl.h>
  34. #ifdef hpux
  35. #include <sys/ptyio.h>
  36. #endif
  37.  
  38. #include <errno.h>
  39. #include <stdio.h>
  40. #include <sys/file.h>
  41.  
  42. FILE *BecomeDaemon (char *filename)
  43. {
  44. #ifdef SERVER
  45.   FILE *logfile;
  46.   register int i;
  47.   int forkresult;
  48.  
  49.   if((logfile=fopen(filename,"a"))==NULL){
  50.     printf("Couldn't create logfile.\n");
  51.     exit(0);
  52.   } 
  53.   fputs("\n========================\n",logfile);    
  54.   fputs("Begin New Server Session\n",logfile);    
  55.   fputs("========================\n\n",logfile);    
  56.   fflush(logfile);
  57.     /*
  58.      * fork so that the process goes into the background automatically. Also
  59.      * has a nice side effect of having the child process get inherited by
  60.      * init (pid 1).
  61.      */
  62.  
  63.     if ( (forkresult = fork ()) ){    /* if parent */
  64.       if(forkresult < 0 ){
  65.         perror("Fork error!");
  66.       }
  67.       exit (0);            /* then no more work to do */
  68.       }
  69.  
  70.     /*
  71.      * Close standard file descriptors and get rid of controlling tty
  72.      */
  73.  
  74.     close (0); 
  75.     close (1);
  76.     close (2);
  77.  
  78.     /*
  79.      * Set up the standard file descriptors.
  80.      */
  81.     (void) open ("/", O_RDONLY);    /* root inode already in core */
  82.     (void) dup2 (0, 1);
  83.     (void) dup2 (0, 2);
  84.  
  85.     if ((i = open ("/dev/tty", O_RDWR)) >= 0) {    /* did open succeed? */
  86. #if defined(SYSV) && defined(TIOCTTY)
  87.     int zero = 0;
  88.     (void) ioctl (i, TIOCTTY, &zero);
  89. #else
  90. #ifndef TIOCNOTTY /********* !!!!!!!!! **********/
  91. #if defined(SVR4)
  92. #include <sys/termios.h>
  93. #else
  94. #include <sys/ttycom.h>
  95. #endif
  96. #endif
  97.     (void) ioctl (i, TIOCNOTTY, (char *) 0);    /* detach, BSD style */
  98. #endif
  99.     (void) close (i);
  100.     }
  101.  
  102.  
  103. #if defined(SYSV) || defined(SVR4)
  104. #if defined(__sun__) || defined(hpux) || defined(sgi) || defined(__osf__)
  105.     setsid();
  106. #else
  107.     setpgrp (0, 0);
  108. #endif
  109. #else /* Non SYSV machines */
  110. #if defined(__osf__) || defined(linux)
  111.     setsid();
  112. #else
  113.     setpgrp (0, getpid());
  114. #endif
  115. #endif
  116.   return(logfile);
  117. #else
  118.   return (NULL);
  119. #endif
  120. }
  121.